home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / Pexample / Create.c < prev    next >
C/C++ Source or Header  |  1990-05-14  |  3KB  |  154 lines

  1. //
  2. // Test performance of thread creation and deletion.  Measure elapsed time
  3. // for NumThreads processors to cooperatively create and delete NumReps
  4. // threads (none of the threads are actually started).
  5. //
  6.  
  7. #include <stdio.h>
  8. #include <stream.h>
  9. #include "presto.h"
  10. #include "StopWatch.h"
  11. #include "atomic_int.h"
  12.  
  13. const int MaxReps = 1000000;
  14. const int MaxThreads = 20;
  15.  
  16. static shared_t int NumReps = 100000;
  17. static shared_t int NumThreads = 1;
  18.  
  19. static shared_t int verbose = 0;
  20.  
  21.  
  22. int
  23. Main::init()
  24. {
  25. //cout << "in main:init\n";
  26.     for (argc--, argv++; *argv && **argv == '-'; argv++, argc--)
  27.         switch (*(*argv + 1))
  28.     {
  29.         case 't':
  30.         case 'p':
  31.         NumThreads = atoi(*argv + 2);
  32.         if ( (NumThreads < 1) || (NumThreads > MaxThreads) )
  33.         {
  34.             cout << form ("Invalid number of threads: %d\n",
  35.                   NumThreads);
  36.             return -1;
  37.         }
  38.         break;
  39.         case 'r':
  40.         NumReps = atoi(*argv + 2);
  41.         break;
  42. //        case 'q':
  43. //        quantum = atoi(*argv + 2);
  44. //        break;
  45.         case 'v':
  46.         verbose++;
  47.         break;
  48.             case 'h':
  49.         cerr << "Usage:  Create [-tN] [-rM] [-v] [-h]\n";
  50.                 return 0;
  51.         default:
  52.         cerr << chr(*(*argv + 1)) << " unknown flag.\n";
  53.         cerr << "Usage:  Create [-tN] [-rM] [-v] [-h]\n";
  54.         return -1;
  55.     }
  56.  
  57.     nummainthreads = NumThreads;
  58.     numprocessors = NumThreads;
  59.  
  60.     cout << "\f";
  61.     cout << "                 Elapsed Time to Create and Delete Threads\n\n";
  62.     cout << form ("Number of threads:    %d\n", NumThreads);
  63.     cout << form ("Number of reps:       %d\n", NumReps);
  64.     cout << "\nworking...\n\n";
  65.     cout.flush ();
  66.  
  67.     return 0;
  68. }
  69.  
  70.  
  71. static shared_t int occupied = 0;
  72. static shared_t Monitor m1("main_monitor 1");
  73. static shared_t Monitor m2("main_monitor 2");
  74. static shared_t HC_AtomicInt ReadyThreads (0);
  75. static shared_t STOPWATCH StopWatch;
  76. static shared_t unsigned long Elapsed;
  77. static shared_t HC_AtomicInt ctr (0);
  78.  
  79.  
  80. Main::main()
  81. {
  82.     int IamFirst = 0;
  83.     Thread* thread;
  84.  
  85.  
  86.     //
  87.     //  Determine first thread - he will be responsible for calculating
  88.     //  the elapsed time of the test.
  89.     //
  90.     {
  91.     MONITOR ENTRY(m1);
  92.         if (!occupied)
  93.     {
  94.         occupied++;
  95.         IamFirst++;
  96.     }
  97.     }
  98.  
  99.     //
  100.     //  Be sure we wait until every thread is up and running before
  101.     //  starting the timer.
  102.     //
  103.     ReadyThreads++;
  104.     while (ReadyThreads < NumThreads) ;
  105.  
  106.     //
  107.     //  Everyone is ready - start timer.
  108.     //
  109.     if (IamFirst) StopWatch.Start ();
  110.  
  111.     //
  112.     //  Repeat until done.
  113.     //
  114. //    while (ctr++ < NumReps)
  115.     while (NumReps-- > 0)
  116.     {
  117.     thread = new Thread ();
  118.     delete thread;
  119.     }
  120.  
  121.     //
  122.     //  All done - wait for all threads to get to this point.
  123.     //
  124.     ReadyThreads--;
  125.     while (ReadyThreads > 0) ;
  126.  
  127.     //
  128.     //  Now that everyone is done, get the elapsed time.
  129.     //  If this processor started stopwatch, have it get the time.
  130.     //
  131.     if (IamFirst) Elapsed = StopWatch.Mark ();
  132.  
  133.     //
  134.     //  Accumulate pairs, report results.
  135.     //
  136.     {
  137.     MONITOR ENTRY(m2);
  138.         cout << "thread done:  " << thisthread->tid() << " - "
  139.              << form ("NumReps = %d\n", NumReps);
  140.         cout.flush ();
  141.     }
  142.  
  143.     return 0;
  144. }
  145.  
  146. int
  147. Main::done()
  148. {
  149.     printf("\n");
  150.     printf("Elapsed time:                  %.2f secs\n",
  151.        (double) Elapsed / 1000000.0);
  152.     return 0;
  153. }
  154.